#!/usr/bin/python
#coding:utf-8

from StringIO import StringIO
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

import requests

class Login(object):
	'''
		login html
	'''

	__home_url = r'http://acm.nyist.net/JudgeOnline/login.php'
	__login_url = r'http://acm.nyist.net/JudgeOnline/dologin.php'

	def __init__(self):
		self.__userid = None
		self.__password = None
		self.__req_session = requests.Session()

	def login(self, u, p):
		'''
			login
		'''
		self.__req_session.get(Login.__home_url)

		self.__userid = u
		self.__password = p

		payload = {
			'userid': self.__userid,
			'password': self.__password
		}

		result = self.__req_session.post(Login.__login_url, params=payload)

		if result.status_code != requests.codes.ok:
			result.raise_for_status()

		return result.text

	def get_profile(self):
		__profile_url = r'http://acm.nyist.net/JudgeOnline/profile.php'

		__request_html = self.__req_session.get(__profile_url)
		
		return __request_html

if __name__ == '__main__':
	data = Login()
	data.login('itssunny', 'hjh8980009')

	f = open("test.html", "w")
	f.write(data.get_profile().content)
	f.close()

